import time


class ProxyModel:
    @staticmethod
    def add_proxy(cursor, proxy, source):
        sql = """
            INSERT INTO proxies (
                `proxy`,
                `source`,
                `status`,
                `create_time`,
                `update_time`
            ) VALUES (
                %(proxy)s,
                %(source)s,
                %(status)s,
                %(create_time)s,
                %(update_time)s
            )
        """
        now = int(time.time())
        params = {
            'proxy': proxy,
            'source': source,
            'status': 1,
            'create_time': now,
            'update_time': now
        }
        return cursor.update(sql, params)

    @staticmethod
    def modify_proxy(cursor, proxy, status):
        now = int(time.time())

        sql = f"""
        UPDATE proxies SET
            `status` = {status},
            `update_time` = {now},
            `fail_time` = {now}
        WHERE
            `proxy` = "{proxy}"
        """
        return cursor.update(sql)
